﻿using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using Equation.Enterprise.Framework.Web.Model;
using System.Collections.Generic;
namespace ENail.Portal.Web.Models
{
    public class EntryViewModel : BaseViewModel<int>
    {
        public int RegistrationId { get; set; }
        public CustomerViewModel Customer { get; set; }
        public AssignmentViewModel Assignment { get; set; }
        public GenericListViewModel<RegistrationListViewItem> Registrations { get; set; }
        public EntryViewModel()
        {
            Customer = new CustomerViewModel();
            Assignment = new AssignmentViewModel();
        }
    }

    public class EntryViewModelMapper
    {
        private readonly IEmployeeService _employeeService;
        private readonly INailServiceService _nailServiceService;
        private readonly ICustomerService _customerService;

        private CustomerViewModelMapper _customerMapper;
        private AssignmentViewModelMapper _assignmentMapper;
        private RegistrationListViewModelMapper _registrationMapper;

        public EntryViewModelMapper(IEmployeeService employeeService, INailServiceService nailServiceService, ICustomerService customerService)
        {
            _employeeService = employeeService;
            _nailServiceService = nailServiceService;
            _customerService = customerService;

            _customerMapper = new CustomerViewModelMapper();
            _assignmentMapper = new AssignmentViewModelMapper(_employeeService, _nailServiceService);
        }
        
        public void MapDomainToView(Registration domain, EntryViewModel view)
        {
            view.RegistrationId = domain.Id;
            if (domain.Id != 0)
                _customerMapper.MapDomainToView(domain.Customer, view.Customer);
            _assignmentMapper.MapDomainToView(domain, view.Assignment);
        }

        public void MapViewToDomain(EntryViewModel view, Registration domain)
        {

            domain.Customer = view.Customer.Id != 0 ? _customerService.GetCustomer(view.Customer.Id) : new Customer();

            _customerMapper.MapViewToDomain(view.Customer, domain.Customer);

            if (view.Assignment.EmployeeId != 0 && view.Assignment.NailServiceId != 0)
                _assignmentMapper.MapViewToDomain(view.Assignment, domain);
        }
    }
}